# -*-coding:Utf-8 -* 
""" Load bar
"""
import bge, bgl, blf, os, aud
from .box import *
from .textbox import *
from .color import *

class buttonOfList(TextBox):
	
	def __init__(self, parent,x, y, text, width = None, height = None):
		self._text = text
		self.activate = True
		self.text_color = ButtonList.DefaultColor
		blf.size(parent.font, parent.size,Font.dpi)
		self._dim = blf.dimensions(parent.font, self.text)
		Box.__init__(self, parent,x, y, width, height)
		self.refresh_textposition()
		self._selected=False
		
	
	def set_size_x(self,value,ratio):
		self._w = value
		for aChild in self.childs:
			aChild.set_size_x(self._w, ratio)
			
	def set_size_y(self,value,ratio):
		for aChild in self.childs:
			aChild.set_size_y(self._h, ratio)
			
	def onClic(self):
		if self.activate:
			self.parent.onClic(self)
		
	@property
	def text(self):
		return self._text
	@text.setter	
	def text(self, value):
		if type(value) != str:
			return
		self._text = value
		self.parent.refresh_dimension()
		self.refresh_textposition()
		
	@property
	def font(self):
		return self.parent._font.Id
	@font.setter	
	def font(self, value):
		return
		
	@property
	def size(self):
		return self.parent.size
	@size.setter	
	def size(self, value):
		return
		
	@property
	def alignement(self):
		return self.parent._alignement
	@alignement.setter	
	def alignement(self, value):
		return
		
	@property
	def padding(self):
		return self.parent._padding
		
	@padding.setter	
	def padding(self, value):
		return
	def _draw(self,x,y,alpha):
		if self.hover or self._selected:
			self.parent.toggle_hm(self)
		TextBox._draw(self,x,y,alpha)
		if self.hover or self._selected:
			self.parent.toggle_hm(self)
			
class ButtonList(Box):
	
	DefaultSize = TextBox.DefaultSize
	DefaultColor = TextBox.DefaultColor
	DefaultAlignement = TextBox.DefaultAlignement
	DefaultPadding = TextBox.DefaultPadding
	DefaultFont = TextBox.DefaultFont
	DefaultHover = {}
	DefaultHovercallback = None
	
	def __init__(self, parent,x, y, textslist, width = None, height = None, background = None):
		Box.__init__(self, parent,x, y, 0, 0,background)
		self.buttons = []
		self._font = ButtonList.DefaultFont
		self._size = ButtonList.DefaultSize
		self._alignement = ButtonList.DefaultAlignement
		self._padding = ButtonList.DefaultPadding
		for aText in textslist:
			grubitext = buttonOfList(self,0,0,aText)
			self.buttons.append(grubitext)
			self._h += grubitext.h
			if grubitext.w > self._w:
				self._w = grubitext.w
		self._minheight = self.h
		self._minwidth = self.w
		if width:
			self.w = width
		if height:
			self.h = height
		if self.w <= self._minwidth:
			self.w = self._minwidth
		if self.h <= self._minheight:
			self.h = self._minheight
		place = self.h
		for aButton in self.buttons:
			aButton.y = place-aButton.h
			place = place-aButton.h
			aButton.w = self.w
		self.hover_modification = ButtonList.DefaultHover.copy()
		self.hover_active = None
		self._onclic = ButtonList._defaultonclic
		self.hovercallback = ButtonList.DefaultHovercallback
			
	def destroy(self):
		Box.destroy(self)
		for aButton in self.buttons:
			aButton.destroy()
		self.buttons.clear()
		
			
	def toggle_hm(self,cible):
		for aKey in self.hover_modification.keys():
			if hasattr(cible,aKey):
				value = getattr(cible,aKey)
				setattr(cible,aKey,self.hover_modification[aKey])
				self.hover_modification[aKey] = value	
		
	def refresh_dimension(self):
		need_refresh = False
		h = 0
		w = 0
		for aButton in self.buttons:
			x,y = aButton.x,aButton.y
			aButton.refresh_dimension()
			if x != aButton.x or y != aButton.y:
				need_refresh = True
			if aButton.w > w:
				w = aButton.w
			h += aButton.h
		if need_refresh or h != self._minheight:
			self._minheight = h
			self._minwidth = w
			if self.w < w:
				self.w = w
			if self.h < h:
				self.h = h
			place = self.h
			for aButton in self.buttons:
				aButton.y = place-aButton.h
				place = place-aButton.h
	
	def refresh_textposition(self):
		for aButton in self.buttons:
			aButton.refresh_textposition()
			
	def _defaultonclic(self, index):
		pass
			
	def onClic(self,button = None):
		if button:
			self._onclic(self,self.buttons.index(button))
			
	@property
	def font(self):
		return self._font.Id
	@font.setter	
	def font(self, value):
		if type(value) == str:
			value = Font(value)
		if type(value) != Font or not value.loaded:
			return
		self._font = value
		self.refresh_dimension()
		self.refresh_textposition()
		
	@property
	def text_color(self):
		return self.buttons[0].text_color
	@text_color.setter	
	def text_color(self, value):
		for aButton in self.buttons:
			aButton.text_color = value
		
	@property
	def size(self):
		return self._size
	@size.setter	
	def size(self, value):
		self._size = int(value)
		self.refresh_dimension()
		self.refresh_textposition()
		
	@property
	def alignement(self):
		return self._alignement
	@alignement.setter	
	def alignement(self, value):
		if not value in (0,1,2):
			return
		self._alignement = value
		self.refresh_textposition()
		
	@property
	def padding(self):
		return self._padding
		
	@padding.setter	
	def padding(self, value):
		if type(value) in (tuple,list):
			if len(value) == 2:
				self._padding = (round(value[0]),round(value[1]),round(value[0]),round(value[1]))
			elif len(value) == 4:
				self._padding = (round(value[0]),round(value[3]),round(value[2]),round(value[1]))
			else:
				value = round(value[0]) 
		if type(value) == int:
			self._padding = (value,value,value,value)
		self.refresh_dimension()
		self.refresh_textposition()
		
	def _draw(self,x,y,alpha):
		hover = None
		if self.hover:
			for aButton in self.buttons:
				if aButton.hover:
					hover = aButton
		if hover != self.hover_active:
			if self.hovercallback != None:
				self.hovercallback(self,hover)
			self.hover_active = hover
		Box._draw(self,x,y,alpha)
